/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package p.lodz.pl.poid.zad2.segmentation;

import java.awt.Color;
import java.util.HashMap;

/**
 *
 * @author Lukasz
 */
public class MaskRegion {

    public HashMap<Integer, QuadNode> QuadeNodeList;
    public int id;
    public Color avrageColor;
    public int pixelCount;

    public MaskRegion(int id) {
        pixelCount = 0;
        this.id = id;
        QuadeNodeList = new HashMap<Integer, QuadNode>();
    }

    public void addNode(QuadNode node) {
        pixelCount += node.pixelCount;
        node.region = this;
        //addToColor(node.avrageColor);
        QuadeNodeList.put(node.position, node);
        calculateAvrageColor();
    }

    private void paddNode(QuadNode node) {
        pixelCount += node.pixelCount;
        node.region = this;
        QuadeNodeList.put(node.position, node);
    }

    public void addRegion(MaskRegion region) {
        pixelCount += region.pixelCount;
        for (QuadNode node : region.QuadeNodeList.values()) {
            this.paddNode(node);
        }
        calculateAvrageColor();
    }

    public void calculateAvrageColor() {
        int avrageColorR = 0;
        int avrageColorG = 0;
        int avrageColorB = 0;
        for (QuadNode node : QuadeNodeList.values()) {
            avrageColorR += node.avrageColor.getRed();
            avrageColorG += node.avrageColor.getGreen();
            avrageColorB += node.avrageColor.getBlue();
        }
        int size = this.QuadeNodeList.size();
        avrageColor = new Color((avrageColorR / size), (avrageColorG / size), (avrageColorB / size));
    }

    public void addToColor(Color color) {
        if (avrageColor == null) {
            avrageColor = new Color(color.getRed(), color.getGreen(), color.getBlue());
            return;
        }
        avrageColor = new Color((avrageColor.getRed() + color.getRed()) / 2, (avrageColor.getGreen() + color.getGreen()) / 2, (avrageColor.getBlue() + color.getBlue()) / 2);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final MaskRegion other = (MaskRegion) obj;
        if (this.id != other.id) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        return id;
    }

    public void Clear() {
        QuadeNodeList = null;
        avrageColor = null;
        pixelCount = 0;
    }
}
